package in.muton.edushare.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.global.SystemGlobals;
import com.edushare.common.util.StringUtil;

/**
 * Servlet Filter implementation class EduShareFilter
 */
public class EduShareFilter implements Filter {
	
	private static final Log log = LogFactory.getLog(EduShareFilter.class);

    public EduShareFilter() {
    }

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
		throws IOException, ServletException {/*
		
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		
		String servletPath = (req).getServletPath();
		servletPath = servletPath.substring(1);
		
		//1. See if there is no slash and page is non-existent
		if(StringUtil.isValid(servletPath)
				&& !SystemGlobals.getInstance().getUrlPaths().contains(servletPath) 
				&& !servletPath.contains("/")
				)
		{
			log.info("nonexisting path:" + servletPath);
			log.info("Forwarding to:"  +URLConstants.USER_PROFILE_PATH +"/" + servletPath);
			req.getRequestDispatcher(URLConstants.USER_PROFILE_PATH +"/" + servletPath).forward(req, res);
		}else{
			log.info("existing path:" + servletPath);
			chain.doFilter(request, response);
		}*/
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
		log.debug("\n" +
				"\n" +
				"---------- EduShare Filter Started ------------\n" +
				"*** This is the top most filter of the App  ***\n" +
				"***    URL redirection takes place here     ***\n" +
				"-----------------------------------------------\n");
	}

}
